package com.yunsc.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.common.exception.YunscException;
import com.yunsc.store.dao.YunscUserAddressDao;
import com.yunsc.store.dto.AddressDTO;
import com.yunsc.store.dto.UserAddressDTO;
import com.yunsc.store.entity.YunscUserAddressEntity;
import com.yunsc.store.entity.YunscUserEntity;
import com.yunsc.store.service.YunscUserAddressService;
import com.yunsc.store.service.YunscUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service("yunscUserAddressService")
public class YunscUserAddressServiceImpl extends ServiceImpl<YunscUserAddressDao, YunscUserAddressEntity> implements YunscUserAddressService {


    @Resource
    private YunscUserService userService;

    @Override
    public List<YunscUserAddressEntity> queryUserAddressList(Integer userId) {
        return this.baseMapper.selectUserAddressList(userId);

    }

    @Override
    public int queryUserDefaultAddress(Integer userId) {
        YunscUserEntity user = userService.getById(userId);
        if (user == null) {
            throw new YunscException("用户不存在");
        }
        return user.getAddressId();
    }

    @Override
    public void setUserDefaultAddress(Integer addressId, Integer userId) {
        userService.update(new UpdateWrapper<YunscUserEntity>()
                .eq("user_id", userId)
                .set("address_id", addressId));
    }

    @Override
    @Transactional
    public void addUserAddress(AddressDTO addressForm, Integer userId, Integer storeId) {
        YunscUserAddressEntity address = new YunscUserAddressEntity();
        BeanUtils.copyProperties(addressForm, address);
        List<AddressDTO.Region> region = addressForm.getRegion();
        // 县
        address.setRegionId(region.get(2).getValue());
        // 市
        address.setCityId(region.get(1).getValue());
        // 省
        address.setProvinceId(region.get(0).getValue());
        address.setStoreId(storeId);
        address.setUserId(userId);
        this.save(address);
        // 获取用户
        int count = this.count(new LambdaUpdateWrapper<YunscUserAddressEntity>()
                .eq(YunscUserAddressEntity::getUserId, userId)
                .eq(YunscUserAddressEntity::getIsDelete, 0));
        if (count == 1) {
            // 设置默认地址
            userService.update(new UpdateWrapper<YunscUserEntity>()
                    .eq("user_id", userId)
                    .set("address_id", address.getAddressId()));
        }



    }

    @Override
    public void editUserAddress(Integer userId, UserAddressDTO params) {
        YunscUserAddressEntity address = new YunscUserAddressEntity();
        BeanUtils.copyProperties(params.getForm(), address);
        UpdateWrapper<YunscUserAddressEntity> updateWrapper = new UpdateWrapper<YunscUserAddressEntity>()
                .eq("user_id", userId)
                .eq("address_id", params.getAddressId());
        List<AddressDTO.Region> region = params.getForm().getRegion();
        // 县
        address.setRegionId(region.get(2).getValue());
        // 市
        address.setCityId(region.get(1).getValue());
        // 省
        address.setProvinceId(region.get(0).getValue());

        this.update(address, updateWrapper);
    }

    @Override
    public void removeUserAddress(UserAddressDTO addressForm, Integer userId) {
        String addressId = addressForm.getAddressId();
        this.update(new UpdateWrapper<YunscUserAddressEntity>()
                .eq("user_id", userId)
                .eq("address_id", addressId)
                .set("is_delete", 1)
        );
    }

    @Override
    public YunscUserAddressEntity queryUserAddressDetail(Integer addressId) {
        YunscUserAddressEntity address = this.baseMapper.selectUserAddressDetail(addressId);
        if (address == null) {
            throw new YunscException("地址不存在");
        }
        return address;
    }


}